/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.htk.web;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.htk.entity.HtkFav;
import com.thinkgem.jeesite.modules.htk.service.HtkFavService;

/**
 * 收藏类Controller
 * 
 * @author htk
 * @version 2018-05-24
 */
@Controller
@RequestMapping(value = "${adminPath}/htk/htkFav")
public class HtkFavController extends BaseController {

	@Autowired
	private HtkFavService htkFavService;

	@ModelAttribute
	public HtkFav get(@RequestParam(required = false) String id) {
		HtkFav entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = htkFavService.get(id);
		}
		if (entity == null) {
			entity = new HtkFav();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = { "list", "" })
	public String listFav(HtkFav htkFav, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtkFav> page = htkFavService.findPage(new Page<HtkFav>(request, response), htkFav);
		model.addAttribute("page", page);
		return "modules/htk/htkFavList";
	}

	@RequiresPermissions("htk:htkFav:view")
	@RequestMapping(value = "form")
	public String form(HtkFav htkFav, Model model) {
		model.addAttribute("htkFav", htkFav);
		return "modules/htk/htkFavForm";
	}

	@RequiresPermissions("htk:htkFav:edit")
	@RequestMapping(value = "save")
	public String save(HtkFav htkFav, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htkFav)) {
			return form(htkFav, model);
		}
		htkFavService.save(htkFav);
		addMessage(redirectAttributes, "保存收藏成功");
		return "redirect:" + Global.getAdminPath() + "/htk/htkFav/?repage";
	}

	@RequiresPermissions("htk:htkFav:edit")
	@RequestMapping(value = "delete")
	public String delete(HtkFav htkFav, RedirectAttributes redirectAttributes) {
		htkFavService.delete(htkFav);
		addMessage(redirectAttributes, "删除收藏成功");
		return "redirect:" + Global.getAdminPath() + "/htk/htkFav/?repage";
	}

	/**
	 * 
	 * @param htkFav
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = { "apilist" })
	@ResponseBody
	public List<HtkFav> apilist() {
		return htkFavService.findList(null);
	}

	/**
	 * 
	 * @param htkFav
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = { "apiSave" })
	@ResponseBody
	public HashMap<String, Object> apiSave(@ModelAttribute HtkFav htkFav) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("message", "收藏成功");
		return map;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = { "apiDelete" })
	@ResponseBody
	public HashMap<String, Object> apiDelete(HtkFav htkFav, RedirectAttributes redirectAttributes) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		htkFavService.delete(htkFav);
		map.put("code", 0);
		map.put("message", "删除成功");
		return map;
	}
}